package com.ln.lnzw.activity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.RegexUtils;
import com.ln.lnzw.R;
import com.ln.lnzw.base.BaseActivity;
import com.ln.lnzw.utils.CacheActivity;
import com.ln.lnzw.utils.ToastFactory;
import com.ln.lnzw.view.ClearableEditText;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by wuwei on 2018/4/13.
 * <p>
 * 忘记密码
 */

public class ForgetPasswordActivity extends BaseActivity {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.phone)
    ClearableEditText phone;
    @BindView(R.id.cet_yanzhengma)
    ClearableEditText cetYanzhengma;
    @BindView(R.id.rl_xiayibu)
    RelativeLayout rlXiayibu;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget_password);
        ButterKnife.bind(this);
        CacheActivity.addActivity(ForgetPasswordActivity.this);
        setView();
    }

    private void setView() {
        phone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                editTextIsYes();
                editTextIsNo();
            }
        });
        cetYanzhengma.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                editTextIsYes();
                editTextIsNo();
            }
        });


    }
    private void editTextIsNo() {
        if (TextUtils.isEmpty(phone.getText())||TextUtils.isEmpty(cetYanzhengma.getText())){
            rlXiayibu.setBackground(getResources().getDrawable(R.drawable.button_bg_no));
        }
    }

    private void editTextIsYes() {
        if (!TextUtils.isEmpty(phone.getText())&&!TextUtils.isEmpty(cetYanzhengma.getText())){
            rlXiayibu.setBackground(getResources().getDrawable(R.drawable.button_bg_yes));
        }
    }
    @OnClick({R.id.iv_back, R.id.rl_xiayibu})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.rl_xiayibu:
                if (TextUtils.isEmpty(phone.getText())||TextUtils.isEmpty(cetYanzhengma.getText())){
                    return;
                }
                if (!TextUtils.isEmpty(phone.getText())&&!TextUtils.isEmpty(cetYanzhengma.getText())){
                    if (!RegexUtils.isMobileExact(phone.getText())){
                        ToastFactory.getToast(activity, "请输入正确的手机号").show();
                        return;
                    }
                    //请求验证码接口判断验证码是否正确
                    if (cetYanzhengma.getText().toString().equals("1111")){
                        Bundle bundle = new Bundle();
                        bundle.putString("phone",phone.getText().toString());
                        bundle.putString("yanzhengma",cetYanzhengma.getText().toString());
                        ActivityUtils.startActivity(bundle,activity, ResetPasswordActivity.class);
                    }else{
                        ToastFactory.getToast(activity, "请输入正确的验证码").show();
                    }
                }
                break;
        }
    }
}
